package com.ctgu.tengyun.handler.mybatis;

import com.ctgu.tengyun.util.MD5Utils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.StringTypeHandler;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ShadowStringTypeHandler extends StringTypeHandler {
	
	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
		ps.setString(i, encrypt(parameter));
	}

	private String encrypt(String value) throws SQLException {
		if( value == null ) {
			return null;
		}
		try {
			return MD5Utils.MD5EncodeUTF8(value);
		} catch (Exception e) {
			throw new SQLException("encrypt error:"+value, e);
		}
	}
	
}
